//
//  SEGOtherCreatActivityRootListViewController.m
//  ExcellentHome
//
//  Created by 陈区 on 13-12-13.
//  Copyright (c) 2013年 陈区. All rights reserved.
//

#import "SEGOtherCreatActivityRootListViewController.h"
#import "SEGCreateActivityViewController.h"


static SEGOtherCreatActivityRootListViewController *sharedViewCtl = nil;
@interface SEGOtherCreatActivityRootListViewController ()
@property (nonatomic, retain)UIView *contentView;
@property (nonatomic, retain)UIView *selectedContentView;

@property(nonatomic,assign)    BOOL isNotification;        //是否是由通知PUSH
@property(nonatomic,retain)    NSDictionary *userInfo;     //接受通知消息体
@end

@implementation SEGOtherCreatActivityRootListViewController

+ (SEGOtherCreatActivityRootListViewController *)sharedViewController
{
    
    if(!sharedViewCtl)
    {
        sharedViewCtl = [[SEGOtherCreatActivityRootListViewController alloc] init];
        sharedViewCtl.hidesBottomBarWhenPushed = YES;
    }
    return sharedViewCtl;
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)setupUI {
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    //广场视图
    UIView *squareView = [[UIView alloc] init];
    squareView.tag = 100;
    [scrollView addSubview:squareView];
    [squareView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(LYScreenWidth());
        make.centerY.mas_offset(0);
    }];
    
    //我的视图
    UIView *myView = [[UIView alloc] init];
    myView.tag = 101;
    [scrollView addSubview:myView];
    [myView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.mas_equalTo(0);
        make.left.equalTo(squareView.mas_right);
        make.width.mas_equalTo(LYScreenWidth());
        make.centerY.mas_offset(0);
    }];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
    
    [self showCustomNavigationBackButton];

    //顶部菜单
    UIView *topBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    self.navigationItem.titleView = topBarView;
    
    //广场按钮
    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, CGRectGetWidth(topBarView.bounds), 20)];
    [titleLabel setText:@"广场"];
    [titleLabel setTextAlignment:NSTextAlignmentCenter];
    [titleLabel setTextColor:[UIColor blackColor]];
    [titleLabel setFont:[UIFont systemFontOfSize:16]];
    [topBarView addSubview:titleLabel];
    
    //分页点
    pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 25, CGRectGetWidth(topBarView.bounds), 20)];
	pageControl.currentPageIndicatorTintColor = kColor_T;
    pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    pageControl.numberOfPages = 2;
    [topBarView addSubview:pageControl];
    
    
    [self showCustomNavigationRightButtonWithTitle:@"发布" image:[UIImage imageWithContentsOfFile:@""] hightlightImage:[UIImage imageWithContentsOfFile:@""] color:kColor_T];
    
    noStartedListCtl = [[SEGOtherCreatActivityListViewController alloc] init];
    noStartedListCtl.viewControllerDelegate=self;
    UIView *bview = [self.view viewWithTag:100];
    [bview addSubview:noStartedListCtl.view];
    [noStartedListCtl.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    myListCtl = [[SEGMyActivityListViewController alloc] init];
    myListCtl.viewControllerDelegate = self;
    UIView *aview = [self.view viewWithTag:101];
    [aview addSubview:myListCtl.view];
    [myListCtl.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if (index == 1) {
        [myListCtl refreshData];
    }else{
        [noStartedListCtl refreshData];
    }

    if (_isNotification==YES) {

        [noStartedListCtl pushNewsDetial:self.userInfo];
        _isNotification=NO;
    }

}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];

}

- (void)onNavigationRightButtonClicked:(UIButton *)sender
{
    
    SEGCreateActivityViewController *createActivityCtl = [[SEGCreateActivityViewController alloc] init];
    createActivityCtl.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:createActivityCtl animated:YES];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark -
#pragma mark uiscrollview delegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int page = scrollView.contentOffset.x/CGRectGetWidth(self.view.bounds);
    pageControl.currentPage = page;
    
    if (pageControl.currentPage==0) {
        [titleLabel setText:@"广场"];
    }else if (pageControl.currentPage==1)
    {
        [titleLabel setText:@"我的"];
        [myListCtl refreshData];
    }
}


#pragma mark- ViewControllerDelegate

//把传过来的界面Push到下一层
- (void)pushViewController:(UIViewController *)aViewController
{
    [self.navigationController pushViewController:aViewController animated:YES];
}

- (void)popToRootViewController:(BOOL)animated
{
    [self.navigationController popToViewController:self animated:YES];
}

@end
